// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.util.list;

import java.lang.reflect.Array;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.RandomAccess;

public final class PZArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess {
    private E[] elements;
    private int numElements;
    private static final PZArrayList<Object> instance = new PZArrayList<>(Object.class, 0);

    public PZArrayList(Class<E> elementType, int initialCapacity) {
        this.elements = (E[])((Object[])Array.newInstance(elementType, initialCapacity));
    }

    public E get(int _int) {
        if (_int >= 0 && _int < this.numElements) {
            return this.elements[_int];
        } else {
            throw new IndexOutOfBoundsException("Index: " + _int + " Size: " + this.numElements);
        }
    }

    public int size() {
        return this.numElements;
    }

    public int indexOf(Object o) {
        for (int _int = 0; _int < this.numElements; _int++) {
            if (o == null && this.elements[_int] == null || o != null && o.equals(this.elements[_int])) {
                return _int;
            }
        }

        return -1;
    }

    public boolean isEmpty() {
        return this.numElements == 0;
    }

    public boolean contains(Object o) {
        return this.indexOf(o) >= 0;
    }

    public Iterator<E> iterator() {
        throw new UnsupportedOperationException();
    }

    public ListIterator<E> listIterator() {
        throw new UnsupportedOperationException();
    }

    public ListIterator<E> listIterator(int index) {
        throw new UnsupportedOperationException();
    }

    public boolean add(E object) {
        if (this.numElements == this.elements.length) {
            int _int = this.elements.length + (this.elements.length >> 1);
            if (_int < this.numElements + 1) {
                _int = this.numElements + 1;
            }

            this.elements = (E[])Arrays.copyOf(this.elements, _int);
        }

        this.elements[this.numElements] = (E)object;
        this.numElements++;
        return true;
    }

    public void add(int int0, E object) {
        if (int0 >= 0 && int0 <= this.numElements) {
            if (this.numElements == this.elements.length) {
                int int1 = this.elements.length + this.elements.length >> 1;
                if (int1 < this.numElements + 1) {
                    int1 = this.numElements + 1;
                }

                this.elements = (E[])Arrays.copyOf(this.elements, int1);
            }

            System.arraycopy(this.elements, int0, this.elements, int0 + 1, this.numElements - int0);
            this.elements[int0] = (E)object;
            this.numElements++;
        } else {
            throw new IndexOutOfBoundsException("Index: " + int0 + " Size: " + this.numElements);
        }
    }

    public E remove(int int0) {
        if (int0 >= 0 && int0 < this.numElements) {
            Object object = this.elements[int0];
            int int1 = this.numElements - int0 - 1;
            if (int1 > 0) {
                System.arraycopy(this.elements, int0 + 1, this.elements, int0, int1);
            }

            this.elements[this.numElements - 1] = null;
            this.numElements--;
            return (E)object;
        } else {
            throw new IndexOutOfBoundsException("Index: " + int0 + " Size: " + this.numElements);
        }
    }

    public boolean remove(Object o) {
        for (int int0 = 0; int0 < this.numElements; int0++) {
            if (o == null && this.elements[int0] == null || o != null && o.equals(this.elements[int0])) {
                int int1 = this.numElements - int0 - 1;
                if (int1 > 0) {
                    System.arraycopy(this.elements, int0 + 1, this.elements, int0, int1);
                }

                this.elements[this.numElements - 1] = null;
                this.numElements--;
                return true;
            }
        }

        return false;
    }

    public E set(int _int, E object1) {
        if (_int >= 0 && _int < this.numElements) {
            Object object0 = this.elements[_int];
            this.elements[_int] = (E)object1;
            return (E)object0;
        } else {
            throw new IndexOutOfBoundsException("Index: " + _int + " Size: " + this.numElements);
        }
    }

    public void clear() {
        for (int _int = 0; _int < this.numElements; _int++) {
            this.elements[_int] = null;
        }

        this.numElements = 0;
    }

    public String toString() {
        if (this.isEmpty()) {
            return "[]";
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append('[');

            for (int _int = 0; _int < this.numElements; _int++) {
                Object object = this.elements[_int];
                stringBuilder.append(object == this ? "(self)" : object.toString());
                if (_int == this.numElements - 1) {
                    break;
                }

                stringBuilder.append(',');
                stringBuilder.append(' ');
            }

            return stringBuilder.append(']').toString();
        }
    }

    public E[] getElements() {
        return this.elements;
    }

    public static <E> AbstractList<E> emptyList() {
        return (AbstractList<E>)instance;
    }
}
